package com.tanhua.server.service;

/**
 * @Author: cl
 * @Date: 2022/5/6 - 05 - 06
 * @Description:
 */

import cn.hutool.core.collection.CollUtil;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.dto.RecommendUserDto;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.vo.CommentVo;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 评论点赞喜欢模块
 */
@Service
@Slf4j
public class CommentService {

    //评论服务
    @DubboReference
    private CommentApi commentApi;

    //用户信息服务
    @DubboReference
    private UserInfoApi userInfoApi;

    //redis服务
    @Autowired
    private RedisTemplate redisTemplate;

    //冻结服务
    @Autowired
    private UserFreezeService userFreezeService;


    /**
     * 发布评论
     *
     * @param movementId
     * @param comment
     */
    public void send(String movementId, String comment) {

        //查询是否被冻结
        userFreezeService.checkUserStatus(2, UserHolder.getUserId());


        //1、获取用户id
        Long userId = UserHolder.getUserId();
        //2、创建并补充comment信息
        Comment comment1 = new Comment();
        comment1.setPublishId(new ObjectId(movementId));
        comment1.setCommentType(CommentType.COMMENT.getType());
        comment1.setUserId(userId);
        comment1.setCreated(System.currentTimeMillis());
        comment1.setContent(comment);
        //3调用服务保存评论
        Integer send = commentApi.send(comment1);
        log.info("commentCount = " + send);
    }

    /**
     * 根据动态id和评论类型分页查询动态评论信息
     *
     * @param page
     * @param pageSize
     * @param movementId
     * @return
     */
    public PageResult findByMovementId(Integer page, Integer pageSize, String movementId) {
        //1、查询动态评论内容
        List<Comment> comments = commentApi.findByMovementId(page, pageSize, new ObjectId(movementId), CommentType.COMMENT);
        //2、判断是否有数据
        if (CollUtil.isEmpty(comments)) {
            return new PageResult();
        }
        //3、查询用户信息
        List<Long> userIds = CollUtil.getFieldValues(comments, "userId", Long.class);
        Map<Long, UserInfo> map = userInfoApi.selectByIds(userIds, new RecommendUserDto());
        //4、封装vo对象并返回
        List<CommentVo> list = new ArrayList<>();
        for (Comment comment : comments) {
            UserInfo userInfo = map.get(comment.getUserId());
            if (userInfo != null) {
                CommentVo init = CommentVo.init(userInfo, comment);

                //当前用户是否对此评论进行点赞
                //修复点赞状态的bug，判断hashKey是否存在
                String key = Constants.MOVEMENTS_INTERACT_KEY + comment.getId().toHexString();
                String hashKey = Constants.MOVEMENT_LIKE_HASHKEY + UserHolder.getUserId();
                if (redisTemplate.opsForHash().hasKey(key, hashKey)) {
                    init.setHasLiked(1);
                }
                list.add(init);
            }
        }
        return new PageResult(page, pageSize, 0l, list);
    }
}
